<script>
	export default {
		onLaunch: async function() {
			this.globalData.sysInfo = uni.getSystemInfoSync()
			let token = uni.getStorageSync('jinxiangguo_login_token')
			this.globalData.token = token
			if (token) {
				uni.request({
					url: this.globalData.requestUrl + '/api/user',
					header: {
						'Authorization': 'Bearer ' + token
					},
					success: (res) => {
						this.globalData.userinfo = res.data.data
					}
				})
				// #ifdef H5
				let sharecode = this.getQueryString('code')
				if (sharecode) {
					this.globalData.helpCode = sharecode
					this.doHelp()
				}
				// #endif
			} else {
				// #ifdef MP-WEIXIN
				const loginPromise = new Promise((resolve, reject) => {
					wx.cloud.init()
					let that = this
					wx.cloud.callFunction({
					    name:'getOpenid'
					}).then(res => {
						// 根据unionid从后台要用户信息
						uni.request({
							url: that.globalData.requestUrl + '/api/user/wxLogin',
							data: {
								unionid: res.result.unionid
							},
							success(resuser) {
								console.log('登录结果', resuser.data)
								that.globalData.unionid = res.result.unionid
								if (resuser.data.data.user) {
									that.globalData.userinfo = resuser.data.data.user
									that.globalData.token = resuser.data.data.token
								}
								resolve()
							}
						})
					}).catch(err => {
					    console.log(err)
						reject(err)
					})
				})
				this.globalData.loginPromise = loginPromise
				// #endif
			}
			
			let audio_bck = uni.getStorageSync('audio_bck')
			let audio_click = uni.getStorageSync('audio_click')
			if (audio_bck === '') audio_bck = 0
			if (audio_click === '') audio_click = 0
			
			const audioBck = uni.createInnerAudioContext()
			audioBck.loop = true
			audioBck.volume = audio_bck
			audioBck.src = 'https://6a69-jinxiangguo-7g6kqddnb0a8aa47-1326817332.tcb.qcloud.la/audio/bck.mp3'
			this.globalData.audioBck = audioBck
			
			const audioClick = uni.createInnerAudioContext()
			audioClick.autoplay = true
			audioClick.volume = audio_click
			audioClick.src = 'https://6a69-jinxiangguo-7g6kqddnb0a8aa47-1326817332.tcb.qcloud.la/audio/click.mp3'
			this.globalData.audioClick = audioClick
			
			setTimeout(() => {
				this.globalData.isFirstOnLoad = false
			}, 1000)
		},
		methods: {
			// #ifdef H5
			preloadImages: async function(urls) {
			  const promises = urls.map(url => {
			    return new Promise((resolve, reject) => {
			      const image = new Image();
			      image.src = url;
			      image.onload = () => resolve(url);
			      image.onerror = reject;
			    });
			  });
			
			  return Promise.all(promises);
			},
			wxinit: function() {
				uni.request({
					url: this.globalData.requestUrl + '/api/user/getJSSDK',
					data: {
						url: location.origin + location.pathname + location.search
					},
					success: (res) => {
						console.log(res.data.data)
						res.data.data.openTagList = ['wx-open-launch-weapp']
						this.$wx.config(res.data.data)
					},
					fail: () => {
						uni.showToast({
							title: '微信配置初始化失败',
							icon: 'fail'
						})
					}
				})
			},
			isWechat: function() {
				var ua = window.navigator.userAgent.toLowerCase();
				if (ua.match(/micromessenger/i) == 'micromessenger') {
					return true;
				} else {
					return false;
				}
			},
			// 正则获取url参数
			getQueryString: function (name) {
			   var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
			   var r = window.location.search.substr(1).match(reg);
			   if(r!=null)return unescape(r[2]); return null;
			},
			// #endif
			doHelp: function() {
				let that = this
				uni.request({
					url: this.globalData.requestUrl + '/api/user/code/save',
					data: {
						code: this.globalData.helpCode
					},
					header: {
						'Authorization': 'Bearer ' + this.globalData.token
					},
					method: 'POST',
					success(res) {
						that.globalData.helpCode = ''
						if (res.data.code === 200) {
							uni.showToast({
								title: '助力成功',
								icon: 'none'
							});
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							});
						}
					}
				})
			},
			playAudio: function () {
				// 播放点击效果和背景音乐
				if (this.globalData.audioBck.paused) {
					this.globalData.audioBck.play()
				}
				if (this.globalData.audioClick.paused) {
					this.globalData.audioClick.play()
				}
			}
		},
		
		onShow: function() {
			if (!this.globalData.isFirstOnLoad && this.globalData.audioBck.paused) {
				this.globalData.audioBck.play()
			}
		},
		onHide: function() {
			if (!this.globalData.audioBck.paused) {
				this.globalData.audioBck.pause()
			}
		},
		globalData: {
			isFirstOnLoad: true, // 是否首次启动
			sysInfo: {}, // 系统信息
			audioBck: {}, // 背景音乐
			audioClick: {}, // 点击音乐
			
			requestUrl: process.env.NODE_ENV === 'development' ? 
				'http://jinxiangguo.tianci.com' :
				'https://www.quxun.fun',
			token: '', // 请求用token
			unionid: '',
			userinfo: {}, // 用户信息
			helpCode: '', // 助力code
			isReloadMarkers: true, // 是否需要重算硬币与圈圈
			mapKey: 'FRDBZ-UU6L3-AOV3E-RSLD3-Y2PJ3-4MBYS' ,// 正式腾讯地图key
			// mapKey: '6I7BZ-SUIKL-GVMPP-EY5NY-3PORV-GYBCH' // 测试腾讯地图key
		}
		
	}
</script>

<style>
	/*每个页面公共css */
	.center {
		display: flex;
		align-items: center;
		justify-content: center;
	}
	uni-toast {
		z-index: 10000;
	}
	uni-modal {
		z-index: 10000;
	}

	.flex {
		display: flex;
	}

	.pr-5 {
		padding-right: 5px;
	}
	/* #ifdef MP-WEIXIN */
	wx-slider .wx-slider-handle-wrapper{
		height: 6px !important;
	}
	wx-slider .wx-slider-handle-wrapper::after {
		    content: "";
		    position: absolute;
		    right: -3px;
		    border-radius: 6px;
		    border: 2px solid #000;
		    box-sizing: border-box;
		    background-color: #C4FF46;
		    width: 12px;
		    height: 12px;
		    top: -3px;
	}
	wx-slider .wx-slider-handle-wrapper::before {
		content: "";
		position: absolute;
		border-radius: 6px;
		border: 2px solid #000;
		box-sizing: border-box;
		background-color: #C4FF46;
		width: 12px;
		height: 12px;
		margin-top: -3px;
		margin-left: -8px;
	}
	wx-slider .wx-slider-thumb {
		border: 2px solid #000 !important;
		box-sizing: border-box;
		border-radius: 6px;
		width: 12px !important;
	}
	/* #endif */
</style>